package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingGeomagneticSearchDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingGeomagneticResDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingGeomagneticReturnDto;
import com.anjubao.parking.municipal.parkinglot.form.ParkingGeomagneticForm;
import com.anjubao.parking.municipal.parkinglot.service.ParkingGeomagneticService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 地磁管理controller
 *
 * @author anjubao
 */
@RestController
@Api(value = "ParkingGeomagneticController", tags = {"地磁感应器管理接口"})
@RequestMapping("/parking-lot-geomagnetics")
public class ParkingGeomagneticController {

    @Autowired
    private ParkingGeomagneticService parkingGeomagneticService;
    private static final Logger logger = LoggerFactory.getLogger(ParkingGeomagneticController.class);
    private static final String CODE="404";

    /**
     * 新增地磁
     *
     * @param parkingGeomagneticForm
     * @return
     */
    @ApiOperation(value = "新增地磁接口", notes = "新增地磁信息")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PostMapping("")
    public ApiResult<ParkingGeomagneticReturnDto> saveParkingGeomagnetic(@RequestBody @Valid @ApiParam(value = "地磁信息") ParkingGeomagneticForm parkingGeomagneticForm) {
        logger.info("新增地磁信息 {} ", parkingGeomagneticForm);
        ParkingGeomagneticReturnDto returnCode = parkingGeomagneticService.insertOne(parkingGeomagneticForm);
        if (returnCode == null) {
            return new ApiResult(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "该泊位已添加地磁");
        }
        if (returnCode.getCode() == CODE) {
            return new ApiResult(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "机器码不能重复");
        }
        return ApiResult.success(returnCode);
    }

    /**
     * 编辑地磁
     *
     * @param parkingGeomagneticForm
     * @return
     */
    @ApiOperation(value = "编辑地磁接口", notes = "编辑地磁信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "设备编号", required = true, dataType = "String"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PutMapping("/{code}")
    public ApiResult<ParkingGeomagneticReturnDto> updateParkingGeomagnetic(@RequestBody @Valid @ApiParam(value = "地磁信息") ParkingGeomagneticForm parkingGeomagneticForm, @PathVariable String code) {
        logger.info("更新地磁信息 {} ", parkingGeomagneticForm);
        ParkingGeomagneticReturnDto returnCode = parkingGeomagneticService.updateOne(parkingGeomagneticForm, code);
        if (returnCode.getCode() == CODE) {
            return new ApiResult(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "机器码不能重复");
        }
        return ApiResult.success(returnCode);
    }

    /**
     * 根据设备编号删除单个地磁信息
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "删除地磁信息接口", notes = "根据设备编号删除单个地磁信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "设备编号", required = true, dataType = "String")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @DeleteMapping("/{code}")
    public ApiResult<ParkingGeomagneticReturnDto> deleteParkingGeomagnetic(@PathVariable String code) {
        logger.info("删除单个地磁信息 {} ", code);
        ParkingGeomagneticReturnDto parkingGeomagnetic = parkingGeomagneticService.removeOne(code);
        return ApiResult.success(parkingGeomagnetic);
    }

    /**
     * 搜索地磁信息(分页)
     * @param parkingGeomagneticSearchDto 搜索参数
     * @param page 当前页
     * @param pageSize 页大小
     * @return
     */
    @ApiOperation(value = "联合查询地磁接口", notes = "查询地磁信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页号", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("")
    public ApiResult<List<ParkingGeomagneticResDto>> search(ParkingGeomagneticSearchDto parkingGeomagneticSearchDto,
                                                            @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        logger.info("查询地磁信息 {} ", parkingGeomagneticSearchDto);
        Page<ParkingGeomagneticResDto> search = parkingGeomagneticService.search(parkingGeomagneticSearchDto, page, pageSize);
        return ApiResult.success(search.getRecords(), (int) search.getTotal());
    }

    /**
     * 根据设备编号查询地磁信息
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "查询单个地磁接口", notes = "查询单个地磁信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "设备编号", required = true, dataType = "String")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/{code}")
    public ApiResult<ParkingGeomagneticForm> selectParkingGeomagnetic(@PathVariable String code) {
        logger.info("查询单个地磁信息 {} ", code);
        ParkingGeomagneticForm parkingGeomagnetic = parkingGeomagneticService.selectOne(code);
        return ApiResult.success(parkingGeomagnetic);
    }

    /**
     * 更新地磁运行状态
     *
     * @param isOnline
     * @param code
     * @return
     */
    @ApiOperation(value = "修改地磁运行状态接口", notes = "根据设备编号删除单个地磁信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isOnline", value = "运行状态 0:断线 1:在线", required = true, dataType = "int"),
            @ApiImplicitParam(name = "code", value = "设备编号", required = true, dataType = "String")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PutMapping("/{code}/change-online")
    public ApiResult<ParkingGeomagneticReturnDto> updateParkingGeomagneticOnline(@RequestParam Integer isOnline, @PathVariable String code) {
        logger.info("修改地磁运行状态 {} ", isOnline, code);
        ParkingGeomagneticReturnDto parkingGeomagnetic = parkingGeomagneticService.updateIsOnline(isOnline, code);
        return ApiResult.success(parkingGeomagnetic);
    }
}
